package lanou3g.myfirstapp36kr.selectplaces;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import lanou3g.myfirstapp36kr.BuildConfig;
import lanou3g.myfirstapp36kr.MyUser;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.activity.AccountMessageActivity;
import lanou3g.myfirstapp36kr.base.BaseActivity;


/**
 * Created by dllo on 16/9/3.
 */
public class PlacesActivity extends BaseActivity implements OnClickListener {
    private CityPicker cityPicker;
    private TextView textViewCancel;
    private TextView textViewSure;
    private MyUser myUser;


    @Override
    protected int getLayout() {
        return R.layout.activity_place;
    }

    @Override
    protected void initView() {
        cityPicker = (CityPicker) findViewById(R.id.citypicker);
        textViewCancel = (TextView) findViewById(R.id.tv_place_cancel);
        textViewSure = (TextView) findViewById(R.id.tv_place_sure);
        myUser = BmobUser.getCurrentUser(MyUser.class);

    }

    @Override
    protected void initData() {
        textViewCancel.setOnClickListener(this);
        textViewSure.setOnClickListener(this);



    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_place_cancel:
                finish();
                break;
            case R.id.tv_place_sure:
                Intent intent = new Intent();
                //if (BuildConfig.DEBUG) Log.d("PlacesActivity", cityPicker.getCity_string());
                String selectedPlaces = cityPicker.getCity_string();
                //上传网络数据地址
                if (myUser != null){
                    myUser.setPlaces(selectedPlaces);
                    myUser.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e == null){
                                Log.d("MyFragment", "上传成功");
                            }else {
                                Log.d("MyFragment", "上传失败:" + e.getMessage());
                            }
                        }
                    });

                }else {
                    Log.d("MyFragment", "myUser == null");
                }
                intent.putExtra("selectedPlaces",selectedPlaces);
                this.setResult(98,intent);
                finish();
                break;
        }
    }
}
